# _*_ coding utf-8 _*_
# FILENAME：requireList.py
# DESCRIPTION: tableview
# AUTH: jarvis zhang
# DATE: 2022/5/3110:53 上午

from PyQt5.QtWidgets import *
from PyQt5.Qt import *
from views.example.tableview1.tableviewUI import TableViewUI
from views.example.tableview1.add_dialog import AddDialog
from views.example.tableview1.upload_dialog import UploadDialog


class TableViewWindow(QMainWindow):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.ui = TableViewUI()
        self.ui.setupUi(self)
        self.ui.btn_upload.clicked.connect(self.upload_click)
        self.ui.btn_edit.clicked.connect(self.add_btn_click)
        self.ui.btn_del.clicked.connect(self.del_data_row)
        self.ui.btn_close.clicked.connect(self.Close)
        # self.ui.btn_query.clicked.connect(self.query_data_list)
        # 添加数据
        newItem = QTableWidgetItem('1')
        # 居中显示
        newItem.setTextAlignment(Qt.AlignHCenter | Qt.AlignVCenter)
        self.ui.data_table.setItem(0, 0, newItem)
        newItem = QTableWidgetItem('赵博')
        newItem.setTextAlignment(Qt.AlignHCenter | Qt.AlignVCenter)
        self.ui.data_table.setItem(0, 1, newItem)
        newItem = QTableWidgetItem('12')
        newItem.setTextAlignment(Qt.AlignHCenter | Qt.AlignVCenter)
        self.ui.data_table.setItem(0, 2, newItem)
        newItem = QTableWidgetItem('5年级二班')
        newItem.setTextAlignment(Qt.AlignHCenter | Qt.AlignVCenter)
        self.ui.data_table.setItem(0, 3, newItem)
        newItem = QTableWidgetItem('优秀')
        newItem.setTextAlignment(Qt.AlignHCenter | Qt.AlignVCenter)
        self.ui.data_table.setItem(0, 4, newItem)

    def Open(self):
        self.show()

    def Close(self):
        self.close()

    # 将新增数据的按钮绑定到该槽函数
    def upload_click(self):
        print('upload')
        '''
        打开上传的弹框模块
        :return:
        '''
        UploadDialog.get_dialog(self)

    # 将新增数据的按钮绑定到该槽函数
    def add_btn_click(self):
        print('add')
        '''
        打开新增数据的弹框模块
        :return:
        '''
        AddDialog.get_add_dialog(self)

    # 将查询数据的按钮绑定到该槽函数
    def query_data_list(self):
        print('query')
        '''
        查询数据、并将数据展示到主窗口的数据列表中
        :return:
        '''
        data = self.data_list
        if len(data) != 0 and len(data[0]) != 0:
            self.data_table.setRowCount(len(data))
            self.data_table.setColumnCount(len(data[0]))
            for i in range(len(data)):
                for j in range(len(data[0])):
                    self.data_table.setItem(i, j, self.ui.QTableWidgetItem(str(data[i][j])))

    # 将删除数据按钮绑定到该槽函数
    def del_data_row(self):
        print('del')
        '''
        删除某一行的数据信息
        :return:
        '''
        row_select = self.data_table.selectedItems()
        print(row_select)
        if len(row_select) != 0:
            row = row_select[0].row()
            print(row)
            self.data_table.removeRow(row)
            del self.data_list[row]
        print(self.data_table)

if __name__ == "__main__":
    import sys
    app = QApplication(sys.argv)
    MainWindow = TableViewWindow()
    MainWindow.show()
    sys.exit(app.exec_())